Android
How to create a Custom Actions
In Huma SDK, Actions are a way to interact with the user. They can be used to display information, ask for user input, or perform an action.
To create new action, you need to create a new class that extends KeyAction
class.
Or BaseKeyAction
for generic look and feel.
class CustomAction : KeyAction() {
override val uniqueKeyActionId: UniqueKeyActionId = "custom_action_id"
override val keyActionId: String = "generic_custom_action_id"
override val title: String = "Custom Action"
override val startDateTime: Instant = Instant.now()
override val endDateTime: Instant = Instant.now().plus(1, ChronoUnit.DAYS)
override val model: String = "Custom Action Model" //used for analytics
override var completedDateTime: Instant? =
null // set to null if not completed, or to the date when it was completed
override var category: KeyActionCategory =
KeyActionCategory.TO_DO // category of the action, used for sorting
@Composable
fun keyActionCard(modifier: Modifier, showCompleted: Boolean) {
// Composable function to display the action card
}
val order: Int get() = Int.MAX_VALUE
val isLocal: Boolean get() = false //used in places in several flows that only require remote actions
fun action(context: Context) {
// Action to be performed, usually this is where you would navigate to an action screen
}
}
How to add the Custom Action to the Action List with the SDK
Create a KeyActionProvider
To add the custom action to the action list, you need create KeyActionProvider
and provide your
custom action through it.
class CustomActionProvider : KeyActionProvider {
override suspend fun fetchKeyActions(datetimeRange: Pair<Instant, Instant>?): List<KeyAction> {
// Fetch the custom actions from the server or local storage with optional datetime range
return listOf(CustomAction())
}
suspend fun markKeyActionRead(keyAction: KeyAction): Boolean {
// Mark the key action as read
return true // return true if the action was marked as read, false otherwise
}
}
Register the KeyActionProvider
in the initializer flow
To register the provider, you need to add your provider to the list of providers in the actionKit initializer.
context.installHumaSdk {
sdk {
actionKit {
providers = listOf(CustomActionProvider())
}
}
}
(Alternatively) Register the KeyActionProvider
in the HumaActionKitManager
Then you need to register the provider in the HumaActionKitManager
instance.
val humaActionKitManager = HumaActionKitManager.getInstance(context)
humaActionKitManager.registerKeyActionProvider(CustomActionProvider())
It is recommended to register the provider in the app startup, for example in the Application
class.
After that, the custom action will be displayed in the action list.
How to add Custom Action to the Action List with the AppKit
If you are extending existing Huma Key Actions, you can use the AppKit
to add your custom action
Create a ImportKeyActionProcessor
To add the custom action to the action list, you need to create ImportKeyActionProcessor
.
class CustomImportKeyActionProcessor : ImportKeyActionProcessor {
override suspend fun processImportedKeyActions(
keyActions: List<KeyAction>,
context: Context,
): List<KeyAction> {
override fun processConfig(keyActionConfigs: List<KeyActionConfig>): List<KeyAction> {
// Process the key action configs and return the key actions
return keyActionConfigs
.filter { it.keyActionId == "custom_action_id" }
.map { CustomAction(...) }
}
suspend fun markKeyActionRead(keyAction: KeyAction): Boolean {
// Mark the key action as read
return true // return true if the action was marked as read, false otherwise
}
}
}
It is recommended to use BaseImportKeyActionProcessor
to avoid boilerplate code. Also it is
providing the markKeyActionRead method by default.
Register the ImportKeyActionProcessor
in the initializer flow
To register the processor, you need to add your processor to the list of processors in the action
block:
context.installHumaSdk {
appkit {
action {
processors = listOf(CustomImportKeyActionProcessor())
}
}
}
(Alternatively) Register the ImportKeyActionProcessor
in the HumaActionsManager
Then you need to register the processor in the HumaActionsManager
instance.
val humaActionsManager = HumaActionsManager.getInstance()
humaActionsManager.registerImportProcessor(CustomImportKeyActionProcessor())
It is recommended to register the processor in the app startup, for example in the Application
After that, the custom action will be displayed in the action list.